package com.hr.controller.user;

import com.hr.domain.vo.CommentVO;
import com.hr.service.CommentService;
import com.hr.utils.Result;
import com.hr.utils.UserContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/comment")
@Tag(name = "用户评论模块")
@AllArgsConstructor
public class CommentController {


    private final CommentService commentService;
    /**
     * 获取房源的评论列表
     * @param houseId 房源id
     * @return 评论列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取房源的评论列表")
    public Result<List<CommentVO>> list(@RequestParam Long houseId) {
        List<CommentVO> comments = commentService.getCommentsByHouseId(houseId);
        return Result.success(comments);
    }
    /**
     * 添加评论
     * @param houseId 房源id
     * AddCommentDTO 评论内容
     */
    @PostMapping("/add")
    @Operation(summary = "添加评论")
    public Result add(@RequestParam Long houseId, @RequestBody AddCommentDTO dto) {
        // 获取当前用户ID
        /*Long currentUserId = 1L;  */
        Long currentUserId = UserContext.getUserId();

        commentService.addComment(houseId, currentUserId, dto.getContent(), dto.getParentId());
        return Result.success("评论成功");

        /*Long currentUserId = UserContext.getUserId();

        try {
            commentService.addComment(houseId, currentUserId, dto.getContent(), dto.getParentId());
            return Result.success("评论成功");
        } catch (IllegalArgumentException e) {
            // 捕获业务异常，返回友好错误
            return Result.error(e.getMessage()); // e.getMessage() 就是 "不能回复自己的评论"
        } catch (Exception e) {
            // 其他异常（如数据库错误）
            return Result.error("评论失败，请稍后再试");
        }*/
    }


    public static class AddCommentDTO {
        private String content;
        private Long parentId; // 回复时传父ID，否则为 null

        public String getContent() { return content; }
        public void setContent(String content) { this.content = content; }
        public Long getParentId() { return parentId; }
        public void setParentId(Long parentId) { this.parentId = parentId; }
    }
}